import { Injectable } from '@angular/core';
import { InMemoryDbService } from 'angular-in-memory-web-api';
import { Hero } from './hero';

@Injectable({
    providedIn: 'root'
})
export class InMemoryDataService implements InMemoryDbService {
    constructor() { }
    createDb() {
        const heroes = [
            { id: 1, name: '李元霸' },
            { id: 2, name: '张飞' },
            { id: 3, name: '赵日天' },
            { id: 4, name: '张三丰' },
            { id: 5, name: '秦琼' },
            { id: 6, name: '吕布' },
            { id: 7, name: '李飞' },
            { id: 8, name: '赵云' },
            { id: 9, name: '陈霸天' },
            { id: 10, name: '孙悟空' }
        ];
        return { heroes };
    }


    genId(heroes: Hero[]): number {
        // return (heroes && heroes.length > 0) ? Math.max(...heroes.map(hero => hero.id)) + 1 : 1;
        return (heroes.length > 0) ? Math.max(...heroes.map(hero => hero.id)) + 1 : 100;
    }
}
